//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by yhlun on 15/11/8.
//  Copyright © 2015年 yhlun. All rights reserved.
//

import UIKit

protocol VisitorLoginViewDelegate : NSObjectProtocol {

    //协议方法 登录
    func visitorWillLogin(
    
        
    )
    //注册
    func visitorWillRegister()
    
}

class VisitorLoginView: UIView {
    
    //声明代理属性
    weak var visitorDelegate : VisitorLoginViewDelegate?
    
    //点击事件 loginDidClick 通过代理 visitorDelegate 调用协议方法 visitorWillLogin
    
    @objc func loginDidBtnClick(){
    
        visitorDelegate?.visitorWillLogin()
    }
    
    @objc func registerDidRegisterClick(){
    
        visitorDelegate?.visitorWillRegister()
    }
    
    //设置页面信息
    func setUIInfo(imageName: String?,title: String){
        
        iconView.hidden = false
        tipLable.text = title
        
        if imageName != nil {
            circleView.image = UIImage(named: imageName!)
            bringSubviewToFront(circleView)
            iconView.hidden = true
        }else {
        
        
            startAnimation()
        }
    }
    
    //设置动画
    
    private func startAnimation() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_2_PI
        anim.duration = 20
        
        //当动画结束 或者试图处于非活跃状态  动画不移除图层
        
        anim.removedOnCompletion = false
        circleView.layer.addAnimation(anim, forKey: nil)
    }

    //重写构造方法
    init() {
    
        super.init(frame: CGRectZero)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置访客视图
    private func setupUI(){
        
        addSubview(circleView)
        addSubview(iconView)
        addSubview(backView)
        
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        //设置 frame 布局失效
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        
        //设置布局
        //VFL
        //公式 view1.attr1 = view2.attr2 * multiplier + constant
        //注意布局的约束尽量相对一个控件
        
        //圆圈的约束
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        //房子的约束
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: circleView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //设置文案的约束
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Top , relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1.0, constant: 26))
        
        //设置文案宽高约束
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //设置登录按钮的约束
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLable, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top , relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置注册按钮的约束
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLable, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top , relatedBy: .Equal, toItem: tipLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //设置背景视图的约束
        //位移枚举 ||   []
        //对视图宽高约束的数值 [String : NSNumber]
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-(-45)-[registerBtn]", options: [], metrics: nil, views: ["backView":backView,"registerBtn":registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginDidBtnClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerDidBtnClick", forControlEvents: .TouchUpInside)
        
    }
   
    
    
    //MARK: 懒加载 所有控件
    private lazy var circleView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var backView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //提示文案
    private lazy var tipLable: UILabel = UILabel(title: "关注一些人，回这里看看有什么,惊喜关注一些人，回这里看看有什么惊喜", size: 14, color: UIColor.lightGrayColor())
    
    private lazy var loginBtn: UIButton = UIButton(title: "登陆", backImage: "common_button_white_disable", color: UIColor.lightGrayColor())
    private lazy var registerBtn: UIButton = UIButton(title: "注册", backImage: "common_button_white_disable", color: UIColor.orangeColor())

}








